Imports DevExpress.Utils

Public Class UcnListBox
    'represents listbox filled with objects, provides drag function

#Region "Attributes"
#Region "Private attributes"
    Private MbReadyForUse As Boolean = False 'indicates proper creation or first initialization of usercontrol
#End Region
#End Region
#Region "Events"
    Public Event MMSelectedIndexChanged() 'event is raised when the selection has been changed - focus has been moved from one item to another
#End Region
#Region "Constructors & Init"
    Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.


        '**********************************************************************
        InitControl()
        '**********************************************************************
    End Sub
    'setting usercontrol settings before its using
    Private Function InitControl() As GeActionResult
        SetReadyForUse(False) 'disabling usercontrol
        Try
            SetReadyForUse(True) 'enabling usercontrol
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'initialization of controls
    Public Function Init(ByVal IoDataCollection As List(Of ClsUcnListBoxItem), ByVal IoImageCollection As ImageCollection, Optional ByVal IoSelectedItem As Object = Nothing) As GeActionResult
        Try
            If MbReadyForUse Then
                'clearing DataSource
                ilbImageListBox.DataSource = Nothing
                'clearing ImageListBox items
                ilbImageListBox.Items.Clear()
                'clearing ImageList images
                ilbImageListBox.ImageList = Nothing

                'setting DataSource
                ilbImageListBox.DataSource = IoDataCollection
                'setting ImageList
                ilbImageListBox.ImageList = IoImageCollection
                'caption of item
                ilbImageListBox.DisplayMember = "Caption"
                'picture of item
                ilbImageListBox.ImageIndexMember = "ImageIndex"
                'select desired item or first one or none
                ilbImageListBox.SelectedIndex = -1
                If IoSelectedItem IsNot Nothing Then
                    SelectItemByData(IoSelectedItem)
                End If

                If ilbImageListBox.SelectedIndex = -1 Then
                    'selecting first item if none is selected
                    If ilbImageListBox.DataSource IsNot Nothing Then
                        ilbImageListBox.SelectedIndex = 0
                    Else
                        ilbImageListBox.SelectedIndex = -1
                    End If
                End If

                Return GeActionResult.kkOk
            Else
                LogEvent("Neinicializovany UcnListBox!", , )
                Return GeActionResult.kkError
            End If
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
#End Region
#Region "Properties"
#Region "Public properties"
    Public ReadOnly Property ReadyForUse() As Boolean
        Get
            Return MbReadyForUse
        End Get
    End Property
#End Region
#End Region
#Region "Events handler"
#Region "ilbImageListBox - Events handler"
    Private Sub ilbImageListBox_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ilbImageListBox.MouseDown
        Try
            'accept only left mouse button
            If Control.MouseButtons = Windows.Forms.MouseButtons.Left Then
                '''''reassigning primary operation
                ''''If Not MoHand.IsChangingPrimaryOperation Then MoHand.PrimaryOperation = ilbActions.SelectedIndex

                Dim LiItemIndexUnderCursor As Integer = ilbImageListBox.IndexFromPoint(New Point(e.X, e.Y))
                'select actual item
                ilbImageListBox.SelectedIndex = LiItemIndexUnderCursor
                'get selected item
                Dim LoDraggedItem As ClsUcnListBoxItem = ilbImageListBox.SelectedItem
                If LoDraggedItem IsNot Nothing AndAlso LoDraggedItem.CanDrag Then
                    'starting draging an item
                    DoDragDrop(LoDraggedItem, DragDropEffects.Copy)
                End If
            End If
        Catch ex As Exception
            LogEventFromException(ex, , , )
        End Try
    End Sub
    Private Sub ilbImageListBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ilbImageListBox.SelectedIndexChanged
        RaiseEvent MMSelectedIndexChanged()
    End Sub
#End Region
#End Region
#Region "Subs"
    'sets MbReadyForUse attribute and sets Enable property of usercontrol
    Private Sub SetReadyForUse(ByVal IbValue As Boolean)
        MbReadyForUse = IbValue
        Me.Enabled = IbValue 'enabling usercontrol
    End Sub
    'selects item by data
    Public Function SelectItemByData(ByVal IoData As Object) As GeActionResult
        Try
            Dim LoDataCollection As List(Of ClsUcnListBoxItem) = CType(ilbImageListBox.DataSource, List(Of ClsUcnListBoxItem))
            For LiI As Integer = 0 To LoDataCollection.Count - 1
                If LoDataCollection.Item(LiI).Data = IoData Then
                    ilbImageListBox.SelectedIndex = LiI
                    Return GeActionResult.kkOk
                End If
            Next

            Throw New Exception("Polozka sa v zozname nenachadza!")
        Catch ex As Exception
            ilbImageListBox.SelectedIndex = -1
            LogEventFromException(ex, , , )
        End Try
    End Function
    'gets data of selected item
    Public Function GetDataOfSelectedItem() As Object
        Dim LoSelectedItem As ClsUcnListBoxItem = ilbImageListBox.SelectedItem
        If LoSelectedItem IsNot Nothing Then
            Return LoSelectedItem.Data
        Else
            Return Nothing
        End If
    End Function
#End Region

End Class